package client.test;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Stack;
import java.util.Vector;

import socket.utils;

import Globle.Global;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ShowDirctory extends Activity {

	private EditText editText1directroy;

	GridView filegrid;
	fileAdapter adapter;
	Context context;
	
	private String Currpath;
	ArrayList<fileinfobean> filelist = new ArrayList<fileinfobean>();
    Stack<arraylistbean>  stack = new Stack<arraylistbean>();
    

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showdirectory);

		context = this;
		editText1directroy = (EditText) findViewById(R.id.editText1directroy);

		adapter = new fileAdapter(this, filelist);
		filegrid = (GridView) findViewById(R.id.fileGrid);
		filegrid.setAdapter(adapter);

		filegrid.setOnItemClickListener(new OnItemClickListener());
		filegrid.setOnItemLongClickListener(new OnlongClickListener());
	}

	public void querydectoryonClicked(View v){
		try {
			String strDir = editText1directroy.getText().toString();
			Currpath = strDir;
			queryfile(Currpath);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class OnItemClickListener implements
			android.widget.AdapterView.OnItemClickListener {
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {

			fileinfobean bean = (fileinfobean) filelist.get(position);
			if (bean.isDir()) {
				try {
					Currpath = Currpath + "\\"+bean.getFilename();
					editText1directroy.setText(Currpath);
					queryfile(Currpath);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				Toast.makeText(context, "onclicked", Toast.LENGTH_SHORT).show();
			}
		}
	}

	class OnlongClickListener implements OnItemLongClickListener {
		public boolean onItemLongClick(AdapterView parent, View v, int postion,
				long id) {

			Toast.makeText(context, "long onclicked", Toast.LENGTH_SHORT)
					.show();
			return true;
		}
	}

	public class fileAdapter extends BaseAdapter {

		private ArrayList<fileinfobean> bean;
		private LayoutInflater layoutInflater;

		public fileAdapter(Context c, ArrayList<fileinfobean> bean) {
			mContext = c;
			this.layoutInflater = LayoutInflater.from(mContext);
			this.bean = bean;
		}

		public int getCount() {
			return bean.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// ImageView imageView;
			infocontorl contorl = null;
			if (convertView == null) {

				contorl = new infocontorl();
				convertView = layoutInflater.inflate(R.layout.fileinfo, null);
				contorl.img = (ImageView) convertView
						.findViewById(R.id.fileimg);
				contorl.text = (TextView) convertView
						.findViewById(R.id.filename);
				convertView
						.setLayoutParams(new AbsListView.LayoutParams(60, 90));
				convertView.setTag(contorl);

			} else {
				contorl = (infocontorl) convertView.getTag();
			}

			contorl.text.setText(bean.get(position).filename);
			if (bean.get(position).isDir()) {
				contorl.img.setBackgroundResource(R.drawable.folder);
			} else {
				contorl.img.setBackgroundResource(R.drawable.text);
			}

			return convertView;
		}

		private Context mContext;

	}

	class infocontorl {
		public ImageView img;
		public TextView text;
	};

	class fileinfobean  implements Serializable {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		boolean isDir;
		String filename;
		String filesize;
		String creatTime;

		public String getFilesize() {
			return filesize;
		}

		public void setFilesize(String filesize) {
			this.filesize = filesize;
		}

		public String getCreatTime() {
			return creatTime;
		}

		public void setCreatTime(String creatTime) {
			this.creatTime = creatTime;
		}

		public boolean isDir() {
			return isDir;
		}

		public void setDir(boolean isDir) {
			this.isDir = isDir;
		}

		public String getFilename() {
			return filename;
		}

		public void setFilename(String filename) {
			this.filename = filename;
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public void queryfile(String strDir) throws UnsupportedEncodingException
	{
		boolean  brecved = false;
	   byte[] sendmsg = new byte[1024];
		sendmsg[0] = Global.REQUESTDIRINFO;
		byte[] content = strDir.getBytes("UNICODE");
		System.arraycopy(content, 2, sendmsg, 4, content.length - 2);

		int count = 0;
		byte[] str = utils.client.sendMsg(new String(sendmsg));

		if (Global.RECVDIRINFO == Global.getMsgId(str)) {
			filelist.clear();
			filelist.trimToSize();
			
			System.arraycopy(str, 4, sendmsg, 0, str.length - 4);
			String strtmppp = new String(sendmsg, "UTF-8");
			String[] strtmp = strtmppp.split("\\|");
			System.out.println(strtmp.length);
			try {
				count = Integer.parseInt(strtmp[1]);
			} catch (Exception e) {
				e.printStackTrace();
				count = 0;
			}
			System.out.println("count=" + count);
			
			if(count > 0){
				brecved = true;
		
			}
			
			while (count > 0) {
				fileinfobean ifo = new fileinfobean();
				byte[] filename = new byte[1024];
				byte[] strs = utils.client.recvMsg(1028);
				System.arraycopy(strs, 4, filename, 0, strs.length - 4);
				String msgTemp = new String(filename, "UTF-8");
				String[] strname = msgTemp.split("\\|");
				System.out.println(strname[0]);
				ifo.setFilename(strname[0]);
				ifo.setFilesize(strname[4]);
				ifo.setDir(strname[2].equals("1") ? true : false);
				ifo.setCreatTime(strname[3]);
				filelist.add(ifo);
				count--;
			}
			if(brecved){
				arraylistbean  bean  = new  arraylistbean();
				bean.list.addAll(filelist);
				bean.cuurpath = strDir;
				stack.push(bean);
				adapter.notifyDataSetChanged();
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		
		if (keyCode == KeyEvent.KEYCODE_BACK){
			if(stack.empty()){
				finish();
			}else{
				    arraylistbean  bean = stack.pop();
				    Currpath   = bean.cuurpath;
					editText1directroy.setText(Currpath);
					filelist.clear();
					filelist.addAll(bean.list); 
					adapter.notifyDataSetChanged();
			}
		}
		return true;
	}
	
	class  arraylistbean{
		public ArrayList<fileinfobean> list = new ArrayList<fileinfobean> ();
		public String cuurpath;	
	}
}
